package com.htbb.model.carInsure;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.htbb.common.base.MappingConfig;
import com.htbb.common.base.exception.BizException;
import com.htbb.common.util.CheckIdCardUtils;
import com.htbb.model.personalInsure.dto.Channel;
import com.htbb.model.personalInsure.entity.CheckInfo;
import com.htbb.order.entity.TcCar;

public class ReqOfCarIsu {
	/*合作伙伴代码*/
	private String partnerCode;
	
	/*交易流水号*/
	private String tradeId; //交易流水号
	private String carModel; //车辆型号
	private String licensePlateNo; //车牌号码
	private String vin; //车辆识别码
	private String engineNo; //发动机号
	private String city; //投保城市
	private String firstRegisterDate; //车的注册日期
	private String isLicense; //是否上牌标志	车辆是否上牌的标志（0 否  1是）
	private String custName; //车辆拥有者
	private String custSsn; //车主身份证号	
	private String custPhone; //车主电话
	private String transferDate; //过户日期
	private String isSame; //是否和车主一样	0代表一样1代表不一样,默认0
	private String isuName; //被保险人姓名
	private String isuSsn; //被保人身份证号
	private String coverageCodes; //险别代码	险别代码必须有商业险，用逗号隔开
	private String glassCode; //玻璃类型代码
	private String days; //天数
	private String isuDtStart; //强险起保日期
	private String isuDtEnd; //强险终保日期
	private String bisuDtStart; //商业险起保日期
	private String bisuDtEnd; //商业险终保日期
	private String limitAmounts; //有些险别需要保额，用逗号隔开，也代表单位保额，例如：A=100000
	private String compensationFreeExcepteds; //是否投保不计免赔标志位	是否投保不计免赔,1：投保不计免赔,2：不投保不计免赔,格式为险别=1或2，例如：L=1
	private String address; //邮寄地址
	private String addrUsername; //收件人姓名
	private String addrPhone; //收件人电话	不填默认投保人
	private String price;//价格	单位：分
	
	public CheckInfo check(){
		Channel channel = MappingConfig.getChannel(partnerCode.toUpperCase());
		
		CheckInfo checkInfo = new CheckInfo();
		/*验证身份证号是否合法*/
		if (!CheckIdCardUtils.validateCard(isuSsn)) {
			throw new BizException("isussn  param  is  invalid");
		}
		if (!CheckIdCardUtils.validateCard(custSsn)) {
			throw new BizException("custSsn  param  is  invalid");
		}
		if(isSame == null || "0".equals(isSame)){
			checkInfo.setIsuName(custName);
			checkInfo.setIsuSsn(custSsn);
			checkInfo.setIsuPhone(custPhone);
		}
		else{
			checkInfo.setIsuName(isuName);
			checkInfo.setIsuSsn(isuSsn);
		}
		checkInfo.setShipNum(licensePlateNo);
		if(StringUtils.isEmpty(addrUsername)){
			checkInfo.setAddrUsername(custName);
		}
		if(StringUtils.isEmpty(addrPhone)){
			checkInfo.setAddrPhone(custPhone);
		}
		
		checkInfo.setIsResgister(channel.getIsResgister());
		checkInfo.setIsuStatus(0);
		checkInfo.setOrdStatus("2");

		checkInfo.setPartnerCode(channel.getPartnerCode());
		checkInfo.setChannelTradeId(channel.getPartnerCode() + "_car_" + this.getTradeId());
		
		checkInfo.setCustPhone(custPhone);
		checkInfo.setCustName(custName);
		checkInfo.setIsuDtStart(isuDtStart);
		checkInfo.setIsuDtEnd(isuDtEnd);
		checkInfo.setBisuDtStart(bisuDtStart);
		checkInfo.setBisuDtEnd(bisuDtEnd);
		checkInfo.setPrice(Long.parseLong(price.trim()));
		checkInfo.setCustSsn(custSsn);
		
		return checkInfo;
	}
	
	public TcCar genTcCar() throws ParseException{
		TcCar tcCar = new TcCar();
		tcCar.setEngineNum(engineNo);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		if(!org.springframework.util.StringUtils.isEmpty(firstRegisterDate)){
			tcCar.setEnrollDate(sf.parse(firstRegisterDate));
		}
		tcCar.setCarOwner(custName);
		tcCar.setOwnerIdNum(custSsn);
		tcCar.setLicenseNum(licensePlateNo);
		tcCar.setFrameNum(vin);
		tcCar.setInCity(city);
		tcCar.setIsLicense(Integer.valueOf(isLicense));
		tcCar.setCarModel(carModel);
		return tcCar;
	}
	
	/**
	 * 获取保障金额map
	 * @return
	 */
	public Map<String,String> getAmountMap(){
		Map<String,String> amountMap = new HashMap<String,String>();//保障金额
		/**险别列表**/
		if(this.getLimitAmounts() != null){
			for(String amount:this.getLimitAmounts().split(",")){
				String[] amountArr = amount.split("=");
				amountMap.put(amountArr[0], amountArr[1]);
			}
		}
		return amountMap;
	}
	
	/**
	 * 不计免赔map
	 * @return
	 */
	public Map<String,String> getFreeExceptedsMap(){
		Map<String,String> freeExceptedsMap = new HashMap<String,String>();//不计免赔
		
		if(this.getCompensationFreeExcepteds() != null){
			for(String free:this.getCompensationFreeExcepteds().split(",")){
				String[] freeArr = free.split("=");
				freeExceptedsMap.put(freeArr[0], freeArr[1]);
			}
		}
		return freeExceptedsMap;
	}
	
	public String getTradeId() {
		return tradeId;
	}
	public void setTradeId(String tradeId) {
		this.tradeId = tradeId;
	}
	public String getCarModel() {
		return carModel;
	}
	public void setCarModel(String carModel) {
		this.carModel = carModel;
	}
	public String getLicensePlateNo() {
		return licensePlateNo;
	}
	public void setLicensePlateNo(String licensePlateNo) {
		this.licensePlateNo = licensePlateNo;
	}
	public String getVin() {
		return vin;
	}
	public void setVin(String vin) {
		this.vin = vin;
	}
	public String getEngineNo() {
		return engineNo;
	}
	public void setEngineNo(String engineNo) {
		this.engineNo = engineNo;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getFirstRegisterDate() {
		return firstRegisterDate;
	}
	public void setFirstRegisterDate(String firstRegisterDate) {
		this.firstRegisterDate = firstRegisterDate;
	}
	public String getIsLicense() {
		return isLicense;
	}
	public void setIsLicense(String isLicense) {
		this.isLicense = isLicense;
	}
	public String getCustName() {
		return custName;
	}
	public void setCustName(String custName) {
		this.custName = custName;
	}
	public String getCustSsn() {
		return custSsn;
	}
	public void setCustSsn(String custSsn) {
		this.custSsn = custSsn;
	}
	public String getCustPhone() {
		return custPhone;
	}
	public void setCustPhone(String custPhone) {
		this.custPhone = custPhone;
	}
	public String getTransferDate() {
		return transferDate;
	}
	public void setTransferDate(String transferDate) {
		this.transferDate = transferDate;
	}
	public String getIsSame() {
		return isSame;
	}
	public void setIsSame(String isSame) {
		this.isSame = isSame;
	}
	public String getIsuName() {
		return isuName;
	}
	public void setIsuName(String isuName) {
		this.isuName = isuName;
	}
	public String getIsuSsn() {
		return isuSsn;
	}
	public void setIsuSsn(String isuSsn) {
		this.isuSsn = isuSsn;
	}
	public String getCoverageCodes() {
		return coverageCodes;
	}
	public void setCoverageCodes(String coverageCodes) {
		this.coverageCodes = coverageCodes;
	}
	public String getGlassCode() {
		return glassCode;
	}
	public void setGlassCode(String glassCode) {
		this.glassCode = glassCode;
	}
	public String getDays() {
		return days;
	}
	public void setDays(String days) {
		this.days = days;
	}
	public String getIsuDtStart() {
		return isuDtStart;
	}
	public void setIsuDtStart(String isuDtStart) {
		this.isuDtStart = isuDtStart;
	}
	public String getIsuDtEnd() {
		return isuDtEnd;
	}
	public void setIsuDtEnd(String isuDtEnd) {
		this.isuDtEnd = isuDtEnd;
	}
	public String getBisuDtStart() {
		return bisuDtStart;
	}
	public void setBisuDtStart(String bisuDtStart) {
		this.bisuDtStart = bisuDtStart;
	}
	public String getBisuDtEnd() {
		return bisuDtEnd;
	}
	public void setBisuDtEnd(String bisuDtEnd) {
		this.bisuDtEnd = bisuDtEnd;
	}
	public String getLimitAmounts() {
		return limitAmounts;
	}
	public void setLimitAmounts(String limitAmounts) {
		this.limitAmounts = limitAmounts;
	}
	public String getCompensationFreeExcepteds() {
		return compensationFreeExcepteds;
	}
	public void setCompensationFreeExcepteds(String compensationFreeExcepteds) {
		this.compensationFreeExcepteds = compensationFreeExcepteds;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getAddrUsername() {
		return addrUsername;
	}
	public void setAddrUsername(String addrUsername) {
		this.addrUsername = addrUsername;
	}
	public String getAddrPhone() {
		return addrPhone;
	}
	public void setAddrPhone(String addrPhone) {
		this.addrPhone = addrPhone;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public String getPartnerCode() {
		return partnerCode;
	}

	public void setPartnerCode(String partnerCode) {
		this.partnerCode = partnerCode;
	}
}
